//
//  DUCalImgButton.swift
//  SimpleCal
//
//  Created by 杜晓祥 on 2021/12/15.
//

import Cocoa

@IBDesignable
class DUCalImgButton: BaseViewButton {
    
    var imageView:NSImageView!
    var mouseView:NSImageView!
    var labelId:String?
    
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        initialize()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        initialize()
    }
    
    private func initialize() {
        imageView = NSImageView()
        imageView.frame = NSRect(x: 5, y: 5, width: 30, height: 30)
        addSubview(imageView)
        mouseView = NSImageView()
        mouseView.frame = NSRect(x: 5, y: 5, width: 30, height: 30)
        mouseView.isHidden = true
        addSubview(mouseView)
    }
    
    override func layout() {
        let w = self.frame.width
        let h = self.frame.height
        let x:CGFloat = w > 30 ? 5 : 2
        imageView.frame = NSRect(x: x, y: x, width: (w - (2 * x)), height: (h - (2 * x)))
        mouseView.frame = NSRect(x: x, y: x, width: (w - (2 * x)), height: (h - (2 * x)))
    }
    
    @IBInspectable var image:NSImage? {
        didSet{
            self.imageView.image = image
        }
    }
    
    @IBInspectable var mouseImage:NSImage? {
        didSet{
            self.mouseView.image = image
        }
    }
    
    override func mouseInEnterd(sender: BaseViewButton, event: NSEvent) {
        if self.mouseView.image == nil {
            self.layer?.backgroundColor = CGColor(red: 255, green: 255, blue: 255, alpha: 0.4)
            self.layer?.cornerRadius = 5
        } else {
            self.imageView.isHidden = true
            self.mouseView.isHidden = false
        }
    }
    
    override func mouseInExited(sender: BaseViewButton, event: NSEvent) {
        if self.mouseView.image == nil {
            self.layer?.backgroundColor = nil
            self.layer?.cornerRadius = 5
        } else {
            self.imageView.isHidden = false
            self.mouseView.isHidden = true
        }
    }
}
